package transaction

import (
	"bytes"
	"crypto/sha256"
	"encoding/gob"
	"goblockchain/utils"
)

type Transaction struct {
	ID        []byte	// 交易的唯一标识id
	TxInputs  []TxInput	// 交易的输入
	TxOutputs []TxOutput	// 交易的输出
}

// caculate transaction hash
func (tx *Transaction) TxHash() []byte {
	var encoded bytes.Buffer
	var hash [32]byte

	encoder := gob.NewEncoder(&encoded)
	err := encoder.Encode(tx)
	utils.Handle(err)

	hash = sha256.Sum256(encoded.Bytes())
	return hash[:]
}

func (tx *Transaction) SetID() {
	tx.ID = tx.TxHash()
}

